#include <iostream>
#include <queue>
#include <cstring>
using namespace std;

const int N = 1010;
char arr[N][N];
int dis[N][N];

int n, m;
int x1, y1, x2, y2;

// 定义方向向量 
int dx[5] = {0, 0, 1, -1};
int dy[5] = {1, -1, 0, 0};

int bfs()
{
    memset(dis, -1, sizeof dis); // -1 表示还没开始搜索
    queue<pair<int, int>> q;
    q.push({x1, y1}); 
    dis[x1][y1] = 0; // 初始化

    while(q.size())
    {
        auto [a, b] = q.front(); q.pop();

        for(int i = 0; i < 4; i++)
        {
            int x = a + dx[i], y = b + dy[i];

            if(x < 1 || y < 1 || x > n || y > m || dis[x][y] != -1 ||  arr[x][y] == '*') continue;

            dis[x][y] = dis[a][b] + 1;
            q.push({x, y});
            if(x == x2 && y == y2) return dis[x][y];
        }
    }
    return -1;
}

int main() 
{
    cin >> n >> m >> x1 >> y1 >> x2 >> y2;
    for(int i = 1; i <= n; i++)
    {
        for(int j = 1; j <= m; j++)
        {
            cin >> arr[i][j];
        }
    }

    cout << bfs() << endl;
    
    return 0;
}
